<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"
         pageEncoding="utf-8"
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <%--<base href="http://localhost:8080/BookStore02/">--%>
    <%--<link type="text/css" rel="stylesheet" href="static/css/common.css" >--%>
    <%@include file="/pages/common/head.jsp" %>
    <%--静态引用,包括项目起始点,和css,jquery等头部信息 --%>

</head>
<body>

<div id="header">
    <img class="logo_img" alt="" src="<%=basepath%>static/img/logo.gif">
    <span class="wel_word">购物车</span>
    <c:if test="${not empty sessionScope.user}">
        <div>
            <span>欢迎<span class="um_span">${sessionScope.user.username}</span>光临尚硅谷书城</span>
            <a href="<%=basepath%>pages/order/order.jsp">我的订单</a>
            <a href="<%=basepath%>userServlet?action=logout">注销</a>&nbsp;&nbsp;&nbsp;
            <a href="<%=basepath%>index.jsp">返回</a>
        </div>
    </c:if>
    <c:if test="${empty sessionScope.user}">
        <div>
            <a href="<%=basepath%>pages/user/login.jsp">登录</a>
            <a href="<%=basepath%>pages/user/regist.jsp">注册</a> &nbsp;&nbsp;
        </div>
    </c:if>
</div>

<div id="main">

    <table>
        <tr>
            <td>商品名称</td>
            <td>数量</td>
            <td>单价</td>
            <td>金额</td>
            <td>操作</td>
        </tr>
        <c:if test="${empty sessionScope}">
            <tr>
                <td colspan="5">
                    <a href="<%=basepath%>pages/user/login.jsp" style="color: red">登陆</a>
                    后,才能查看购物车
                </td>
            </tr>
        </c:if>
        <c:if test="${not empty sessionScope}">
            <c:if test="${empty sessionScope.cart.items}">
                <tr>
                    <td colspan="5">购物车为空,
                        <a href="<%=basepath%>" style="color: red">去首页</a>
                    </td>
                </tr>
            </c:if>
            <c:if test="${not empty sessionScope.cart.items}">
                <c:forEach items="${sessionScope.cart.items}" var="entry">
                    <tr>
                        <td>${entry.value.name}</td>
                        <td>
                            <input class="updateCount" style="width: 30px;" type="text" value="${entry.value.count}"
                                   goodsId="${entry.value.id}">
                        </td>
                        <td>${entry.value.price}</td>
                        <td>${entry.value.totalPrice}</td>
                        <td><a class="deleteClass"
                               href="<%=basepath%>cartServlet?action=deleteCartItem&cartItemId=${entry.value.id}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
            </c:if>
        </c:if>
    </table>
    <c:if test="${not empty sessionScope}">
        <c:if test="${not empty sessionScope.cart.items}">
            <div class="cart_info">
                <span class="cart_span">购物车中共有<span class="b_count">${sessionScope.cart.totalCount}</span>件商品</span>
                <span class="cart_span">总金额<span class="b_price">${sessionScope.cart.totalPrice}</span>元</span>
                <span class="cart_span"><a class="clearCart"
                                           href="<%=basepath%>cartServlet?action=clear">清空购物车</a></span>
                <span class="cart_span"><a href="<%=basepath%>orderServlet?action=createOrder">去结账</a></span>
            </div>
        </c:if>
    </c:if>
    <script>
        $(function () {
            $("a.clearCart").click(function () {
                return confirm("确认清空吗?");
            })
            $("input.updateCount").change(function () {
                if (confirm("确定修改" + $(this).parent().parent().find("td:first").text() + "数量为" + $(this).val() + "吗?")) {
                    location.href = "<%=basepath%>cartServlet?action=updateCount&goodsId=" + $(this).attr("goodsId") + "&count=" + $(this).val();
                } else {
                    $(this).val(this.defaultValue);
                    //this.value=this.defaultValue;
                }
                console.log("输入框内容改变事件")
            })
        })
    </script>

</div>

<%--        <div id="bottom">--%>
<%--            <span>--%>
<%--                尚硅谷书城.Copyright &copy;2015--%>
<%--            </span>--%>
<%--        </div>--%>
<%@include file="/pages/common/foot.jsp" %>
<%--静态引用,公共底部--%>
</body>
</html>